#!/bin/bash
###
# @Descripttion:
# @version:
# @Author: TT
# @Date: 2023-01-03 16:28:46
# @LastEditors: TT-hzy
# @LastEditTime: 2024-05-14 17:16:04
###
# shellcheck disable=SC2154
# shellcheck source=/dev/null
source ./hzy_rob_tool.sh
source ./hzy_rob_config.sh
chmod a+x hzy_rob_tool.sh hzy_rob_config.sh
# ==================== 自动化创建项目 ==================== #
# ==================== 前提 获取脚本参数 ==================== #
# 进入到项目根目录
echomsg "前提配置参数"
configParams "$@"
normalParamsExplain
project_path="$path/$project_name"
# 创建项目
createFlutterProject() {
    cd "$path" || exit
    if [ ! -d "$project_name" ]; then
        echomsg "第一步"
        echo "${template}创建中..."
        flutter create --org "$org" -i "$ilanguage" -a "$alanguage" --platforms="$platforms" --pub "$project_name"
        project_path="$path/$project_name"
        echo '项目创建完成'
    else
        echomsg "紧急‼️  提示"
        read -r -n 1 -p "项目已存在,是否删除并重新创建> y/n:" char
        printf "\n"
        if [ "$char" == "y" ]; then
            echo '正在删除中...'
            cd "$path" || exit
            rm -rf "$project_name"
            echo '删除完成'
        else
            read -r -p "请重新输入项目名字:" pname
            printf "\n"
            echo "$pname"
            if [ "$pname" != "$project_name" ]; then
                project_name="$pname"
                project_path="$path/$project_name"
            fi
            read -r -p "请重新输入项目包名:" porg
            printf "\n"
            echo "$porg"
            if [ "$porg" != "$org" ]; then
                org=$porg
            fi
        fi
        createFlutterProject
    fi
}

# 创建资源
mkdirAssets() {
    echomsg "第二步"
    echo '资源文件创建中...'
    project_path="$path/$project_name"
    # 进入到项目根目录
    cd "$project_path" || exit
    createDir "assets"
    cd "assets" || exit
    for item in "${assetsChildarr[@]}"; do
        createDir "$item"
    done
    tree
    echo "资源文件创建完成"
}

# 创建整体项目文件
createNeedLib() {
    echomsg "第三步"
    echo "项目文件创建中..."
    lib="$project_path/lib"
    cd "$lib" || exit

    case "${level}" in
    "normal")
        url="https://gitee.com/hot_night/hzy_depend_project.git"
        pName="hzy_depend_project"
        ;;
    "mini")
        url="https://gitee.com/hot_night/hzy_original_project.git"
        pName="hzy_original_project"
        ;;
    *) ;;
    esac
    git clone "$url"
    cd "$pName/" || exit
    cp -r "lib" "$project_path/"
    cp - r "assets" "$project_path/"
    cp -r "pubspec.yaml" "$project_path/"
    cp -r ".gitignore" "$project_path/"
    sed -i "" "s/$pName/$project_name/g" "$project_path/pubspec.yaml"
    cd "$lib" || exit
    rm -rf "$pName"
}

modifyTest() {
    echomsg "第四步"
    echo "修改test文件中..."
    cd "$project_path" || exit
    pwd
    createFileAndWriteContent "$project_path/test/widget_test.dart" "import 'package:$project_name/init/init.dart';"
    addContentToEndOnly "$cr_test" "$project_path/test/widget_test.dart"
    echo "test文件修改完成"
}
configUpdata() {
    cd "$project_path" || exit
    echomsg "第五步"
    echo "更新项目"
    flutter clean
    flutter pub get
}

openProject() {
    echomsg "第六步"
    echo '打开项目'
    if ! type Visual Studio Code >/dev/null 2>&1; then
        userAndroidStudioOpen
    else
        useVsCodeOpenPorject
    fi
}

runProject() {
    cd "$project_path" || exit
    echomsg "第七步"
    echo "启动web中..."
    flutter run -d chrome

}
createFlutterProject
mkdirAssets
createNeedLib
modifyTest
configUpdata
openProject
echomsg '演示完成'
